/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lunify;

import java.util.Arrays;

/**
 *
 * @author katie
 */
public class Row{
    private String[] data;
    public Row(String... data){
        this.data = data;
    }
    boolean include(String... filter){
        for(int i=0; i<filter.length && i<data.length; i++){
            if(filter[i]!=null && !filter[i].equals(data[i])){
                return false;
            }
        }
        return true;
    }

    @Override
    public int hashCode() {
        return Arrays.hashCode(data);
    }
    
    public int compareTo(Row other, int... order){
        for(int i : order){
            int v = this.data[i].compareTo(other.data[i]);
            //System.out.println(Arrays.toString(data)+" "+v+" "+Arrays.toString(other.data)+" over "+i);
            if(v!=0){
                return v;
            }
        }
        return 0;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Row other = (Row) obj;
        if (!Arrays.deepEquals(this.data, other.data)) {
            return false;
        }
        return true;
    }
    
    public String toString(String base, String replace, int... order){
        StringBuilder sb = new StringBuilder();
        int from = 0;
        int idx = base.indexOf(replace, from);
        int ct = 0;
        while(idx!=-1){
           // System.out.println(base+" "+replace+" "+from+" "+idx+" "+ct+" "+order[ct]+" "+sb);
            sb.append(base.substring(from,idx)).append(data[order[ct]]);
            from = idx+1;
            idx = base.indexOf(replace, from);
            ct++;
        }
        return sb.toString();
    }
    /*
    public static void main(String args[]){
        Row a = new Row("one","two","three");
        System.out.println(a.toString("_ <> _", "_", 0, 1));
        System.out.println(a.include("one",null,null));
        System.out.println(a.include("five",null,null));
    }
     * 
     */
    
    public String[] getData(){
        return data;
    }
    
    public boolean hasWhiteSpace(int... relevant){
        for(int i : relevant){
            for(int c=0; c<data[i].length(); c++){
                if(Character.isWhitespace(data[i].charAt(c))){
                    return true;
                }
            }
        }
        return false;
    }
}